<?php

/**
 * @param $template_id
 * @param $touser
 * @param $url
 * @param $data
 */
function xayc_send_msg_tpl($template_id, $touser, $url, $data)
{
    $jencode = file_get_contents("http://xawx.xayc.com.cn/index.php?g=Home&m=Weixin&a=pub_getAccessToken&appid=wxc9d635475d9f373a");
    $jencode = strrev($jencode);
    $token = json_decode($jencode, true);

    slog($token);

    $response = xayc_curl("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token['access_token'], json_encode([
        'template_id' => $template_id,
        'touser' => $touser,
        'url' => $url,
        "topcolor" => "#FF0000",
        "data" => $data
    ]));

    slog($response);
    return json_decode($response, true);
}

/**
 * @param $requestUrl
 * @param $content
 * @return mixed
 */
function xayc_curl($requestUrl, $content)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $requestUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $exec = curl_exec($ch);
    return $exec;
}
